package pers.wwz.ftp.utils;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import java.io.*;
import java.util.UUID;

import static pers.wwz.ftp.utils.FtpUtil.storeFile;

public class Test {

    public static void main(String[] args) throws Exception {
        // 上传文件测试
        FTPClient ftpClient = new FTPClient();
        boolean connectServer = FtpUtil.connect(ftpClient, "60.64.3.4", 21, "dacs", "Zycfc@2024");
        if (!connectServer) {
            System.out.println("与ftp建立连接失败");
            return;
        }
        File file = new File("E:\\tmp\\飞书接入-20240126180337034.xlsx");
        // 文件转为流
        InputStream inputStream = new FileInputStream(file);
        // 必须这样设置才行
        boolean b = storeFile(ftpClient, new String("/e-record/飞书接入-20240126180337034-.xlsx".getBytes(), FTP.DEFAULT_CONTROL_ENCODING), inputStream);
        System.out.println(b);

        // 下载文件测试
        boolean connectServerDownload = FtpUtil.connect(ftpClient, "60.64.3.4", 21, "dacs", "Zycfc@2024");


        OutputStream outputStream = null;

        String uuidDown = UUID.randomUUID().toString().replace("-", "");
        // 调用 transferTo 方法传输文件内容
        File fileDown = File.createTempFile(uuidDown, ".heic");

        outputStream = new FileOutputStream(fileDown);
        String hookLocaltion = "/e-record";
        ftpClient.changeWorkingDirectory(new String(hookLocaltion.getBytes(), FTP.DEFAULT_CONTROL_ENCODING));

        Boolean downloadFile = ftpClient.retrieveFile(new String("/e-record/1.xlsx".getBytes(), FTP.DEFAULT_CONTROL_ENCODING), outputStream);

        System.out.println(downloadFile);
    }
}
